/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * Class Resource nằm trong package Common
 * Class cung cấp nguồn ảnh, nguồn nhạc cho game
 */
package ClassFrame;

import GamePlay.CanvasGame;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;

/**
 *
 * @author QuyetNM1
 */
public class Resource {

    private CanvasGame cParent;
    //Color
    public static final int c_WHITE = 0xffffff;
    public static final int c_BG = 0xFFFFFF;
    public static final int c_Menu = 0xFFFFFF;
    public static final int c_Focus = 0x000000;
    // fonts
    public static final Font f0 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
    public static final Font f_NORMAL = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
    public static final Font f_MENU = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
    public static final Font f_BOLD = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
    public static final Font f_ITALIC = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_LARGE);
    public static final Font f_BOLD_ITALIC = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD | Font.STYLE_ITALIC, Font.SIZE_LARGE);
    public static final Font f_UNDERLINE = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
    public static final Font bigBoldFont = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_BOLD, Font.SIZE_LARGE);
    // images
    private static int count = 0;
    //Splash
    public static final int IMG_SPLASH = count++;
    public static final int IMG_BG_MENU = count++;
    public static final int IMG_BG_MENU2 = count++;
    public static final int IMG_BG_OTHER = count++;
    public static final int IMG_ABOUT = count++;
    public static final int IMG_WELCOME1 = count++;
    public static final int IMG_WELCOME2 = count++;
    public static final int IMG_WELCOME3 = count++;
    public static final int IMG_WELCOME4 = count++;
    public static final int IMG_WELCOME5 = count++;
    public static final int IMG_CONTINUE = count++;
    public static final int IMG_BG_PLAY = count++;
    public static final int IMG_BTN_CHOIGAME = count++;
    public static final int IMG_BTN_LT = count++;
    public static final int IMG_BTN_KHUNG = count++;
    public static final int IMG_BTN_ABOUT = count++;
    public static final int IMG_BTN_HIGHSCORE = count++;
    public static final int IMG_BTN_EXIT = count++;
    public static final int IMG_BTN_SOUND = count++;
    public static final int IMG_BTN_SOUND_PLAY = count++;
    public static final int IMG_RULE1 = count++;
    public static final int IMG_RULE2 = count++;
    public static final int IMG_RULE3 = count++;
    //Choose Level
    public static final int IMG_BG_CL = count++;
    public static final int IMG_BTN_LEVEL = count++;
    public static final int IMG_BTN_LEVEL_LOCK = count++;
    //Choose Piece
    public static final int IMG_FRAME = count++;
    public static final int IMG_FRAME_3 = count++;
    public static final int IMG_BTN_PIECE = count++;
    public static final int IMG_BTN_PIECE_3 = count++;
    public static final int IMG_BTN_PIECE_SPECIAL = count++;
    public static final int IMG_PIECE2 = count++;
    //play
    public static final int IMG_OCHU = count++;
    public static final int IMG_NUMBER = count++;
    public static final int IMG_NUMBER2 = count++;
    public static final int IMG_HINT = count++;
    public static final int IMG_BG_NOTICE = count++;
    //sound type
    private static final String MIDTYPE = "audio/midi";
    private static final String WAVTYPE = "audio/x-wav";
    private static final String MP3TYPE = "audio/mp3";
    public static final String[] imgNames = {
        //Splash
        "/menu/splash.png",
        "/menu/bg_menu.png",
        "/menu/bg_menu2.png",
        "/menu/bg_other.png",
        "/menu/about.png",
        "/menu/welcome1.png",
        "/menu/welcome2.png",
        "/menu/welcome3.png",
        "/menu/welcome4.png",
        "/menu/welcome5.png",
        "/menu/continue.png",
        "/menu/bg_play.png",
        "/button/btn_choigame.png",
        "/button/btn_lt.png",
        "/button/khung.png",
        "/button/btn_about.png",
        "/button/btn_highscore.png",
        "/button/btn_exit.png",
        "/button/btn_amthanh.png",
        "/button/btn_amthanh_play.png",
        "/menu/rule1.png",
        "/menu/rule2.png",
        "/menu/rule3.png",
        //choose level
        "/cl/bg.png",
        "/cl/level.png",
        "/cl/level_lock.png",
        //choose piece
        "/cl/frame.png",
        "/cl/frame_3.png",
        "/cl/piece.png",
        "/cl/piece_3.png",
        "/cl/btnSpecialPiece.png",
        "/cl/piece2.png",
        //play
        "/menu/ochu.png",
        "/menu/num.png",
        "/menu/num2.png",
        "/menu/goiy.png",
        "/menu/thongbao.png"};
    //max imgs
    private static final int MAX_IMG = imgNames.length;
    public static Image[] img; // image array
    private static final String[] soundsNames = {
        "/sound/menu.mid",
        "/sound/play.mid",
        "/sound/button.mp3",
        "/sound/correct.mp3",
        "/sound/wrong.mp3",
    };
    //sounds
    public static final int SOUND_MENU = 0;
    public static final int SOUND_PLAY = 1;
    public static final int SOUND_BUTTON = 2;
    public static final int SOUND_CORRECT = 3;
    public static final int SOUND_WRONG = 4;
    
    public static final int MAX_SOUND = soundsNames.length;
    public Sound[] sounds;
    public static final int[] splashArrayImages = {IMG_SPLASH}; 
    public static final int[] aboutArrayImages = {IMG_ABOUT, IMG_BTN_LT}; 
    public static final int[] ruleArrayImages = {IMG_RULE1, IMG_RULE2, IMG_RULE3};
    public static final int[] menuArrayImages = {IMG_BTN_SOUND, IMG_BG_MENU, IMG_BG_MENU2, IMG_BTN_CHOIGAME, IMG_BTN_LT, IMG_BTN_KHUNG, IMG_BTN_ABOUT, IMG_BTN_HIGHSCORE, IMG_BTN_EXIT};
    public static final int[] highScoreArrayImages = {IMG_BG_OTHER, IMG_BTN_LT};
    public static final int[] welcomeArrayImages = {IMG_CONTINUE, IMG_WELCOME1, IMG_WELCOME2, IMG_WELCOME3, IMG_WELCOME4, IMG_WELCOME5, IMG_BTN_LT};
    public static final int[] chooseLevelArrayImages = {IMG_NUMBER2, IMG_BG_CL, IMG_BTN_LEVEL, IMG_BTN_LEVEL_LOCK, IMG_BG_NOTICE, IMG_BTN_LT};
    public static final int[] choosePieceArrayImages = {IMG_NUMBER, IMG_BG_PLAY, IMG_FRAME, IMG_FRAME_3, IMG_BTN_PIECE, IMG_BTN_PIECE_3, IMG_BTN_PIECE_SPECIAL, IMG_BG_NOTICE, IMG_BTN_LT};
    public static final int[] gameArrayImages = {IMG_BG_PLAY, IMG_BTN_LT, IMG_OCHU, IMG_NUMBER, IMG_PIECE2, IMG_HINT, IMG_BTN_SOUND_PLAY};

    //Contructor
    public Resource(CanvasGame _canvas) {
        this.cParent = _canvas;
        this.img = new Image[MAX_IMG];
        this.sounds = new Sound[MAX_SOUND];
    }

    //Lấy ảnh từ mảng Image
    public Image getIMG(int _code) {
        return img[_code];
    }

    /**
     * Load resources
     */
    public boolean load(int i) {
        /* TODO: load the images here */
        try {
            img[i] = Image.createImage(imgNames[i]);
        } catch (Exception ex) {
            System.err.println(ex.toString());
            System.out.println(i);
            return false;
        }
        return true;
    }

    /**
     * Unload resources
     */
    public void unLoad(int i) {
        /* TODO: make sure the object get's destroyed */
        img[i] = null;
    }

    public void loadArray(int[] imgs) {
        if (imgs == null) {
            return;
        }
        for (int i = 0; i < imgs.length; i++) {
            load(imgs[i]);
        }
    }

    public void unLoadArray(int[] imgs) {
        if (imgs == null) {
            return;
        }
        for (int i = 0; i < imgs.length; i++) {
            unLoad(imgs[i]);
        }
    }

    public void loadSounds() {
        for (int i = 0; i < MAX_SOUND; i++) {
            loadSound(i);
        }
    }

    public void unLoadSounds() {
        for (int i = 0; i < MAX_SOUND; i++) {
            unLoadSound(i);
        }
    }
    
    public static int iSound = -1;
    
    public void loadSound(int i) {
//        Sound sound = null;
        if(sounds[i] != null)   return;
        try {
            String abc = soundsNames[i].substring(soundsNames[i].length() - 3, soundsNames[i].length());
            if(abc.equals("mid")){
                sounds[i] = Sound.createSound(getClass().getResourceAsStream(soundsNames[i]), MIDTYPE);
                sounds[i].repeat(-1);
            }else if(abc.equals("wav")){
                sounds[i] = Sound.createSound(getClass().getResourceAsStream(soundsNames[i]), WAVTYPE);
                sounds[i].repeat(1);
            }else if(abc.equals("mp3")){
                sounds[i] = Sound.createSound(getClass().getResourceAsStream(soundsNames[i]), MP3TYPE);
                sounds[i].repeat(1);
            }
            
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public void unLoadSound(int i) {
        if(sounds[i] != null){
            sounds[i].release();
            sounds[i] = null;
        }
        
    }
    
    public void unLoadAllSound() {
        for (int i = 0; i < MAX_SOUND; i++) {
            if (sounds[i] != null) {
                sounds[i].release();
                sounds[i] = null;
            }
        }
    }
    
    public void playSound(int name, int repeat){
        
        try {
            String abc = soundsNames[name].substring(soundsNames[name].length() - 3, soundsNames[name].length());
            loadSound(name);
            if(abc.equals("mid")){
                if(iSound != name){
                    iSound = name;
                } else {
                    return;
                }
            }
//            if(!CanvasGame.isSound) return;
//            if(abc.equals("mid")){
//                sounds[name].setVolumeLevel(40);
//            } else if(abc.equals("wav")){
//                sounds[name].setVolumeLevel(40/3);
//            } else if(abc.equals("mp3")){
//                sounds[name].setVolumeLevel(40/3);
//            }
            
//            sounds[name].repeat(repeat);
            
            if(name==3){
                sounds[name].setVolumeLevel(CanvasGame.musicLevel/4);
            } else {
                sounds[name].setVolumeLevel(CanvasGame.musicLevel);
            }
            
            sounds[name].start();
            System.out.println("play sound " + name);
        } catch (MediaException ex) {
            System.out.println(ex.toString() + " Error while playing sound " + name);
        }

    }


    public Sound getSound(int code) {
        return sounds[code];
    }

    public void setSounds(Sound sounds, int code) {
        this.sounds[code] = sounds;
    }
}
